$(document).ready(
		function() {
		
			var generalCampaignFields = 
			[{
				"label" : "Campaign Name:",
				"name" : "CAMPAIGN_NAME"
			}, {
				"label" : "Reason Code:",
				"name" : "REASON_CODE",
				"type" : "select",
				"ipOpts" : [ {
					"label" : "windeln.de",
					"value" : "WINDELN_DE"
				}, {
					"label" : "windelbar",
					"value" : "WINDELBAR"
				}, {
					"label" : "windeln.ch",
					"value" : "WINDELN_CH"
				} ]
			}, {
				"label" : "UTM Source:",
				"name" : "UTM_SOURCE"
			}, {
				"label" : "UTM Medium:",
				"name" : "UTM_MEDIUM"
			}, {
				"label" : "AT-Internet Code:",
				"name" : "AT_INTERNET_CODE"
			}, {
				"label" : "UTM Campaign:",
				"name" : "UTM_CAMPAIGN"
			}, {
				"label" : "Campaign Start:",
				"name" : "START_DATE",
				"type" : "date",
				"dateFormat" : 'yy-mm-dd'
			}, {
				"label" : "Campaign End:",
				"name" : "END_DATE",
				"type" : "date",
				"dateFormat" : 'yy-mm-dd'
			}, {
				"label" : "Fix Costs:",
				"name" : "FIX_COSTS"
			}, {
				"label" : "Variable:",
				"name" : "VARIABLE"
			}, {
				"label" : "Cost per Variable:",
				"name" : "COST_PER_VARIABLE"
			}, {
				"label" : "Number of Variable:",
				"name" : "NUMBER_OF_VARIABLE"
			}, {
				"label" : "Campaign Description:",
				"name" : "DESCRIPTION",
				"type" : "textarea"
			}];
			
			var promocodeFields = [{
					"label" : "Pro_StartDate:",
					"name" : "Pro_StartDate",
					"type" : "date",
					"dateFormat" : 'yy-mm-dd'
				}, {
					"label" : "Pro_EndDate:",
					"name" : "Pro_EndDate",
					"type" : "date",
					"dateFormat" : 'yy-mm-dd'
				}, {
					"label" : "Pro_Zuordnung:",
					"name" : "Pro_Zuordnung",
					"default" : "P"
				}, {
					"label" : "Pro_Type:",
					"name" : "Pro_Type"
				}, {
					"label" : "Pro_Nutzniesser:",
					"name" : "Pro_Nutzniesser",
					"type" : "select",
					"ipOpts" : [ {
						"label" : "Neukunde",
						"value" : "Neukunde"
					}, {
						"label" : "Stammkunde",
						"value" : "Stammkunde"
					}, {
						"label" : "Alle",
						"value" : "Alle"
					} ]
				}, {
					"label" : "Pro_ValueProvided:",
					"name" : "Pro_ValueProvided"
				}, {
					"label" : "Pro_ValueType:",
					"name" : "Pro_ValueType",
					"type" : "select",
					"ipOpts" : [ {
						"label" : "Euro",
						"value" : "Euro"
					}, {
						"label" : "%",
						"value" : "Prozent"
					} ]
				}];
			
			editor = new $.fn.dataTable.Editor({
				// "display" : "lightbox",
				"ajaxUrl" : "campaigns",
				"domTable" : "#example",
				 "idSrc": "ID",
				// "fnServerData": function ( sSource, aoData, fnCallback ) {
				"ajax" : function(method, ajaxUrl, aoData, fnCallback,
						errorCallback) {
					if (aoData.action === 'create') {
						$.ajax({
							dataType : 'json',
							contentType : "application/json;charset=UTF-8",
							type : 'POST',
							url : ajaxUrl,
							data : JSON.stringify(aoData.data),
							success : function(json) {
								window.location.reload();
//								fnCallback({ID: 5001});
							},
							error : function (xhr, error, thrown) {
								var results = $.parseJSON(xhr.responseText);
								if(results.errors != undefined){
									$.each(results.errors, function(index){
										editor.error(results.errors[index].key, results.errors[index].value);
									});	
								}else{
									alert("Error on Server");	
								}
					          errorCallback( xhr, error, thrown );
							}
					});
					} else if (aoData.action === 'remove') {
						$.ajax({
							type : 'DELETE',
							url : ajaxUrl + "/"
									+ oTable.fnGetData(currentOpenenedRow).ID,
							success : function(json) {
//								fnCallback();
								window.location.reload();
							},
							error : function (xhr, error, thrown) {
								var results = $.parseJSON(xhr.responseText);
								if(results.errors != undefined){
									$.each(results.errors, function(index){
										editor.error(results.errors[index].key, results.errors[index].value);
									});	
								}else{
									alert("Error on Server");	
								}
					          errorCallback( xhr, error, thrown );
							}
						});
					} else if (aoData.action === 'edit') {
						aoData.data.ID = aoData.id;
						
						$.ajax({
							dataType : 'json',
							contentType : "application/json;charset=UTF-8",
							type : 'PUT',
							url : ajaxUrl,
							data : JSON.stringify(aoData.data),
							success : function() {
//								fnCallback();
								window.location.reload();
							},
							error : function (xhr, error, thrown) {
								var results = $.parseJSON(xhr.responseText);
								if(results.errors != undefined){
									$.each(results.errors, function(index){
										editor.error(results.errors[index].key, results.errors[index].value);
									});	
								}else{
									alert("Error on Server");	
								}
					          errorCallback( xhr, error, thrown );
							}
						});
					}
				},
				// 'onPreSubmit' : function(data){
				//				 
				// var $form = $(".DTE_Action_Edit form").first();
				// var validator = $form.validate({
				// rules: {
				// 'DTE_Field_UTM_SOURCE' : "required",
				// DTE_Field_START_DATE : {
				// required : true
				// }
				// }
				// ,
				// messages: {
				// DTE_Field_UTM_SOURCE : {
				// requried : "muss ausgew�hlt sein"
				// }
				// }
				// ,
				// success: function(label) {
				// alert("sucessful validated");
				// }
				// });
				// console.log(validator);
				// console.log($(".DTE_Action_Edit form").first().valid());
				// // validator.showErrors(data);
				// }
				// },
				// "fnDrawCallback": function() {
				// $(oTable).click(function () {
				// alert('blah');
				// var anSelected = fnGetSelected(oTable);
				// alert (anSelected);
				// oTable.fnDeleteRow( anSelected[0]);
				// });
				// }
				// ,
				events : {
					onInitComplete : function(){
					 console.log("onInitComplete"); 
				 }
				}
				,
				"fields" : [ 
					            generalCampaignFields, 
					            {
									"label" : "Promocode:",
									"name" : "Pro_CodeName"
				             	}, 
				             	promocodeFields
			             	]

			});
			
//			$( 'input', $("#DTE_Field_Pro_CodeName")).on( 'change', function () {
//				console.log($("#DTE_Field_Pro_CodeName"));
//			});
			
			editor.on('onInitEdit', function() {
				editor.disable('COST_PER_VARIABLE');
				editor.disable('NUMBER_OF_VARIABLE');
				editor.disable('FIX_COSTS');
			});
				
			
			$("#DTE_Field_Pro_CodeName").live('keyup',function(e){
				var procodeNameValue = $(this).val();
				$.each( promocodeFields, function( key, value ) {
					var $currentPromoCode = $(".DTE_Field_Name_" + value.name);
					if(procodeNameValue == ''){
						$currentPromoCode.hide();//{'duration' : 0})
					}else{
						$currentPromoCode.show();
					}
				});
			});
			
			 editor.on('onOpen', function(){
				 console.log();
				 setTimeout(function(){
					$.each( promocodeFields, function( key, value ) {
						var $currentPromoCode = $(".DTE_Field_Name_" + value.name);
						
						var procodeNameValue = $("#DTE_Field_Pro_CodeName").val();
						if(procodeNameValue == ''){
							$currentPromoCode.hide();//{'duration' : 0})
						}else{
							$currentPromoCode.show();
						}
					});
				 }, 600);
			 }
			 );
			
			
			editor.on('onClose', function() {
				$(".modal-header button").detach();
			});
			
			


		});